/*
 This file 'theme' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2015 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2015-04-08 14:47
 */

define(function (require, exports, module) {
	var theme = {};

	theme.init = function () {
		//var _theme = firebird.data.get("fb.theme") || "default";

		// 过滤器注册，用于主题过滤
		firebird.filter.register({
			name: "themeFilter",
			pattern: "*",
			callback: function (req, chain, jq, win) {
				var _theme = firebird.data.get("fb.theme") || "default";
				jq("html").removeAttr("class").addClass("fb-ui-theme-" + _theme);
				setTimeout(function () {
					win.fb.use([
						"_fbClassic_/css/theme/" + _theme + ".css",
						"_fbClassic_/" + _theme + "/css/theme/" + _theme + ".css"
					], function (theme1, theme2) {
						firebird.info("Theme: " + _theme + "loaded! detail: ", theme1, theme2);
					});
				}, 0);
				// try {
				// 	jq("head").append("<link href='" + (fb.resolve("_fbClassic_/css/theme/" + _theme + ".css")) + "' rel='stylesheet' />");
				// } catch (e) {
				// }
				// try {
				// 	jq("head").append("<link href='" + (fb.resolve("_fbClassic_/" + _theme + "/css/theme/" + _theme + ".css")) + "' rel='stylesheet' />");
				// } catch (e) {
				// }
			}
		});

		// 过滤器注册，用于页面浏览器版本过滤
		firebird.filter.register({
			name: "browserFilter",
			pattern: "*",
			callback: function (req, chain, jq, win) {

				var brow = $.browser;
				var bInfo = "";
				if (brow.msie) {
					bInfo = "ie fb-ui-ie-" + brow.version;
				}
				if (brow.mozilla) {
					bInfo = "ff fb-ui-ff-" + brow.version;
				}
				if (brow.safari) {
					bInfo = "safari fb-ui-safari-" + brow.version;
				}
				if (brow.opera) {
					bInfo = "opera fb-ui-opera-" + brow.version;
				}
				if (brow.chrome) {
					bInfo = "chrome fb-ui-chrome-" + brow.version;
				}
				jq("html").addClass(("fb-ui-" + bInfo).replace(/\./g, "-"));
			}
		});

		theme.apply();
	};

	theme.apply = function () {
		var _theme = firebird.data.get("fb.theme") || "default";

		$("html").removeAttr("class").addClass("fb-ui-theme-" + _theme);
		require.async([
			"_fbClassic_/css/theme/" + _theme + ".css",
			"_fbClassic_/" + _theme + "/css/theme/" + _theme + ".css"
		], function (theme1, theme2) {
			firebird.info("Theme: " + _theme + "loaded! detail: ", theme1, theme2);
		});
		$("iframe").each(function () {
			var iframe = $(this);
			try {
				var html = iframe.contents().find("html"),
					head = html.find("head");
				html.removeAttr("class").addClass("fb-ui-theme-" + _theme);
				head
					.append("<link href='" + (fb.resolve("_fbClassic_/css/theme/" + _theme + ".css")) + "' rel='stylesheet' />")
					.append("<link href='" + (fb.resolve("_fbClassic_/" + _theme + "/css/theme/" + _theme + ".css")) + "' rel='stylesheet' />");
			} catch (ex) {
				firebird.warn(ex);
			}
		});

	};

	module.exports = theme;
});